package light.mvc.service.record.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import light.mvc.dao.BaseDaoI;
import light.mvc.model.record.Tholiday;
import light.mvc.pageModel.base.PageFilter;
import light.mvc.pageModel.base.Tree;
import light.mvc.pageModel.record.Holiday;
import light.mvc.service.record.HolidayServiceI;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class HolidayServiceImpl implements HolidayServiceI {
	@Autowired
	private BaseDaoI<Tholiday> holidayDao;

	public void add(Holiday r) {
		Tholiday t = new Tholiday();
		t.setIsdefault(Integer.valueOf(1));
		t.setName(r.getName());
		t.setSeq(r.getSeq());
		t.setDescription(r.getDescription());
		this.holidayDao.save(t);
	}

	public void delete(Long id) {
		Tholiday t = (Tholiday) this.holidayDao.get(Tholiday.class, id);
		this.holidayDao.delete(t);
	}

	public void edit(Holiday r) {
		Tholiday t = (Tholiday) this.holidayDao.get(Tholiday.class, r.getId());
		t.setDescription(r.getDescription());
		t.setName(r.getName());
		t.setSeq(r.getSeq());
		this.holidayDao.update(t);
	}

	public Holiday get(Long id) {
		Tholiday t = (Tholiday) this.holidayDao.get(Tholiday.class, id);
		Holiday r = new Holiday();
		r.setDescription(t.getDescription());
		r.setId(t.getId());
		r.setIsdefault(t.getIsdefault());
		r.setName(t.getName());
		r.setSeq(t.getSeq());
		return r;
	}

	public List<Holiday> dataGrid(Holiday project, PageFilter ph) {
		List<Holiday> ul = new ArrayList();
		Map<String, Object> params = new HashMap();
		String hql = " from Tholiday t ";
		List<Tholiday> l = this.holidayDao.find(hql + whereHql(project, params) + orderHql(ph), params, ph.getPage(), ph.getRows());
		for (Tholiday t : l) {
			Holiday u = new Holiday();
			BeanUtils.copyProperties(t, u);
			ul.add(u);
		}
		return ul;
	}

	public Long count(Holiday project, PageFilter ph) {
		Map<String, Object> params = new HashMap();
		String hql = " from Tholiday t ";
		return this.holidayDao.count("select count(*) " + hql + whereHql(project, params), params);
	}

	private String whereHql(Holiday project, Map<String, Object> params) {
		String hql = "";
		if (project != null) {
			hql = hql + " where 1=1 ";
			if (project.getName() != null) {
				hql = hql + " and t.name like :name";
				params.put("name", "%%" + project.getName() + "%%");
			}
		}
		return hql;
	}

	private String orderHql(PageFilter ph) {
		String orderString = "";
		if ((ph.getSort() != null) && (ph.getOrder() != null)) {
			orderString = " order by t." + ph.getSort() + " " + ph.getOrder();
		}
		return orderString;
	}

	public List<Tree> tree() {
		List<Tholiday> l = null;
		List<Tree> lt = new ArrayList();

		l = this.holidayDao.find("select distinct t from Tholiday t order by t.seq");
		if ((l != null) && (l.size() > 0)) {
			for (Tholiday r : l) {
				Tree tree = new Tree();
				tree.setId(r.getId().toString());
				tree.setText(r.getName());
				lt.add(tree);
			}
		}
		return lt;
	}

	public void grant(Holiday project) {
	}
}
